package com.wuwei.business.service.impl;

import com.alibaba.fastjson.JSON;
import com.wuwei.business.Constant.CarConstant;
import com.wuwei.business.VO.BackVo;
import com.wuwei.business.VO.RentVo;
import com.wuwei.business.domain.BusCar;
import com.wuwei.business.domain.BusCustomer;
import com.wuwei.business.mapper.BusCarMapper;
import com.wuwei.business.mapper.BusCustomerMapper;
import com.wuwei.business.utils.SnUtil;
import com.wuwei.common.core.domain.model.LoginUser;
import com.wuwei.common.utils.SecurityUtils;
import com.wuwei.common.utils.bean.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.wuwei.business.domain.BusRent;
import com.wuwei.business.mapper.BusRentMapper;
import com.wuwei.business.service.BusRentService;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 内容: SSM
 * 作者: weishao
 */
@Service
public class BusRentServiceImpl implements BusRentService{

    @Resource
    private BusRentMapper busRentMapper;
    @Autowired
    private SnUtil snUtil;
    @Autowired
    private BusCarMapper busCarMapper;
    @Autowired
    private BusCustomerMapper busCustomerMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return busRentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BusRent record) {
        return busRentMapper.insert(record);
    }

    @Override
    public int insertSelective(BusRent record) {
        return busRentMapper.insertSelective(record);
    }

    @Override
    public BusRent selectByPrimaryKey(Integer id) {
        return busRentMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BusRent record) {
        return busRentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BusRent record) {
        return busRentMapper.updateByPrimaryKey(record);
    }

    /**
     * 分页查询出租单
     * @param loginUser
     * @param busRent
     * @return
     */
    @Override
    public List<BusRent> selectRentPage(LoginUser loginUser, BusRent busRent) {
        return busRentMapper.selectRentPage(loginUser,busRent);
    }

    /**
     * 通过账单号查询订单
     * @param rentSn
     * @return
     */
    @Override
    public BackVo checkRentSn(String rentSn) {
        BusRent busRent = busRentMapper.selectBusRentByrentSn(rentSn);
        if (ObjectUtils.isEmpty(busRent)){
            throw new IllegalArgumentException("出租单号不存在");
        }
        if (busRent.getRentStatus().equals(CarConstant.CAR_REBACK)){
            throw new IllegalArgumentException("该订单已归还");
        }
        BackVo backVo = new BackVo();
        String username = SecurityUtils.getUsername();
        String sn = snUtil.createSn(CarConstant.CHECK_PREFIX);
        BusCustomer busCustomer = busCustomerMapper.selectByIdentity(busRent.getIdentity());
        BusCar busCar=busCarMapper.selectCarByCarNum(busRent.getCarNumber());
        backVo.setOpername(username);
        backVo.setRentSn(busRent.getRentSn());
        backVo.setCheckSn(sn);
        backVo.setCustomer(busCustomer);
        backVo.setCar(busCar);
        backVo.setRent(busRent);
        return backVo;
    }

}
